/*
 * Copyright 2010 J Patrick Davenport
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v4view.core;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;

import java.io.IOException;

import org.junit.Test;

public class ElementTests {
	private CleanRoomElement	element	= new CleanRoomElement();

	@Test
	public void checkSettingAttribute() {
		final String name = "title";
		final String value = "A great title";

		this.element.setAttribute(name, value);
		final Attribute attribute = this.element.getAttribute(name);
		assertNotNull(attribute);
		assertEquals(name, attribute.getName());
		assertEquals(value, attribute.getValue());

		this.element.removeAttribute(name);
		final Attribute attribute2 = this.element.getAttribute(name);
		assertNull(attribute2);
	}

	@Test
	public void checkSettingAttributeTwice() {
		final String name = "title";
		final String value = "A great title";

		this.element.setAttribute(name, value);
		this.element.setAttribute(name, value);
		final Attribute attribute = this.element.getAttribute(name);
		assertNotNull(attribute);
		assertEquals(name, attribute.getName());
		assertEquals(value, attribute.getValue());

		this.element.removeAttribute(name);
		final Attribute attribute2 = this.element.getAttribute(name);
		assertNull(attribute2);
	}

	@Test
	public void checkEquality() {
		final Attribute a = new Attribute("title", "a");
		final Attribute b = a;

		assertTrue(a.equals(b));
		final Attribute c = new Attribute("title", "a");
		assertTrue(c.equals(a));

		assertEquals(c.hashCode(), b.hashCode());

		assertFalse(c.equals(null));
	}

	@Test
	public void checkHasAttributes() {
		assertFalse(this.element.hasAttributes());
		this.element.setAttribute("title", "A title");
		assertTrue(this.element.hasAttributes());
	}

	@Test
	public void checkIllegalArgumentAttribute() {
		boolean threwException = false;
		try {
			this.element.setAttribute(null, "");
		} catch (final IllegalArgumentException ex) {
			threwException = true;
		}

		assertTrue(threwException);
	}

	@Test
	public void supportsAttributes() {
		assertTrue(this.element.supportsAttributes());
	}

	@Test
	public void renderTheAttributes() {
		final String renderAttribute = this.element.renderAttribute();
		assertEquals("", renderAttribute);

		final String value = "Here's a title";
		final String name = "title";
		this.element.setAttribute(name, value);
		assertEquals(String.format("%s=\"%s\" ", name, value), this.element.renderAttribute());
	}

	@Test(expected = AttributesNotSupportedException.class)
	public void doesNotSupportAttributes() {
		final CleanRoomNoSupportElement cleanRoomNoSupportElement = new CleanRoomNoSupportElement();
		assertFalse(cleanRoomNoSupportElement.supportsAttributes());
		cleanRoomNoSupportElement.hasAttributes();
	}

	private static class CleanRoomElement extends Element {
		public CleanRoomElement() {
			this.setSupportsAttributes(true);
		}

		@Override
		public void load() {

		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see v4view.core.Element#render()
		 */
		@Override
		public void render() throws IOException {
			throw new UnsupportedOperationException("Lazy developer didn't do anything");
		}

	}

	private static class CleanRoomNoSupportElement extends Element {

		@Override
		public void load() {

		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see v4view.core.Element#render()
		 */
		@Override
		public void render() throws IOException {
			throw new UnsupportedOperationException("Lazy developer didn't do anything");
		}

	}
}
